/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package services;
//import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import common.Configuration;

import models.QuestionGroupDTO;

/**
 *
 * @author User
 */
public class QuestionGroupServiceImpl implements IEntity
{
	Statement stmt=null;
    ResultSet rst=null;
    private QuestionGroupDTO questionGroup;
    private List<QuestionGroupDTO> questionGroupList;

    public QuestionGroupDTO GetById(int id) {
    	
        try {
        	
            stmt = Configuration.GetStatement();
            rst = stmt.executeQuery("select * from questiongroup where idQuestiongroup=" + id); 
            questionGroup.setidQuestionGroup(id);
            questionGroup.setQGTitle(rst.getString("QGTitle"));
            questionGroup.setQGDesc(rst.getString("QGDesc"));
            questionGroup.setIdQuestionType(rst.getInt("idQuestionType"));
            Configuration.CloseConnection();

        } catch (SQLException ex) {
            Logger.getLogger(QuestionGroupServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return questionGroup;
    }

    public List<QuestionGroupDTO> GetAll() {
        
    	try {
        	
        	stmt = Configuration.GetStatement();
            rst = stmt.executeQuery("select * from questiongroup ");
            while (rst.next()) {
            	
                questionGroup.setidQuestionGroup(rst.getInt("idQuestionGroup"));
                questionGroup.setQGTitle(rst.getString("QGTitle"));
                questionGroup.setQGDesc(rst.getString("QGDesc"));
                questionGroup.setIdQuestionType(rst.getInt("idQuestionType"));
                questionGroupList.add(questionGroup);
            }
            Configuration.CloseConnection();
            
        } catch (SQLException ex) {
            Logger.getLogger(QuestionGroupServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
      return  questionGroupList;
    }

    public void Add(Object obj) {
    	
        QuestionGroupDTO objCast=(QuestionGroupDTO)obj;
        
        try {
			
        	stmt = Configuration.GetStatement();
			stmt.execute("INSERT INTO dbsurvey.questiongroup (`QGTitle`, `QGDesc`, `idQuestionType`) VALUES ("+objCast.getQGTitle()+","+objCast.getQGDesc()+", "+objCast.getIdQuestionType()+")");
			Configuration.CloseConnection();
        
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    public void Update(Object obj)  {
    	
        QuestionGroupDTO objCast=(QuestionGroupDTO)obj;
        try {
			
        	stmt = Configuration.GetStatement();
        	stmt.execute("update dbsurvey.questiongroup set  QGTitle="+objCast.getQGTitle()+"and QGDesc="+objCast.getQGDesc()+"and idQuestionType="+objCast.getIdQuestionType()+" where idQuestionGroup="+objCast.getidQuestionGroup());
			Configuration.CloseConnection();
    
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
        
    public void Remove(int id){
    	try {
			stmt = Configuration.GetStatement();
			stmt.execute("delete from questiongroup where idQuestionGroup =" + id);
			Configuration.CloseConnection();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}
